<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class cron extends core {
	private $order_mod;
	
	public function __construct() {
		parent::__construct();
		$this->order_mod = m("order");
	}

	//取消24小时内没有支付的订单
	public function order_cancel() {
		$now = time();
		$hours_24 = $now - 24 * 3600;
		$sql = "UPDATE {$this->tbp}order SET status='-1', cancel_time='{$now}' WHERE status='0' AND pay_time='0' AND add_time<='{$hours_24}'";
		$this->db->query($sql);
		$this->write_log('order_auto_cancel');
	}

	//发货超过指定天数，系统确认收货，默认7天
	public function order_shouhuo() {
		$auto_shouhuo_days = isset($this->configs['auto_shouhuo_days']) ? intval($this->configs['auto_shouhuo_days']) : 7;
		$day7 = time() - (($auto_shouhuo_days-1)*24*3600);
		$sql = "SELECT * FROM {$this->tbp}order WHERE shipping_time<='{$day7}' AND shouhuo_time=0";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $k => $g) {
				$this->order_mod->shouhuo($g->id, $g->member_id);
			}
		}
		$this->write_log('order_auto_shouhuo');
	}

	//已经完成的订单，默认一天后好评，5星
	public function order_comment() {
		$after_one_day = date("Y-m-d H:i", time() + 24*3600);
		$sql = "SELECT * FROM {$this->tbp}order WHERE status='3' AND shouhuo_time<'{$after_one_day}'";
		//echo $sql."<br>";
		$orders = $this->db->get_results($sql);
		$this->write_log('set 5 stars');
		if ($orders) {
			foreach ($orders as $key => $order) {
				$sql = "SELECT * FROM {$this->tbp}order_goods WHERE order_id='{$order->id}'";
				$rs = $this->db->get_results($sql);
				if ($rs) {
					//如果还没评论
					if ($rs->comment_time == 0) {
						$sql = "UPDATE {$this->tbp}order_goods SET comment_stars='5', comment_time='".time()."', 
							comment_content='评价方未及时做出评价,系统默认好评!', member_id='{$order->member_id}'
							WHERE id='{$rs->id}'";
						$this->db->query($sql);
					}
				}
				//改变订单状态
				$sql = "UPDATE {$this->tbp}order SET status='4' WHERE id='{$order->id}'";
				$this->db->query($sql);
			}
		}
	}

	//24小时后自动确认退货退款申请
	public function order_refund() {
		$now = time();
		$day7 = $now - 7*24*3600;
		$sql = "SELECT * FROM {$this->tbp}order_refund WHERE add_time<='{$day7}' AND status='0' AND add_time>'0'";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			require_once(APP_PATH . '/member.php');
			$member = new member();
			foreach ($rs as $k => $g) {
				$member->order_check_return_goods($g->order_id, '自动确认退货或退款', 1, false);
			}
		}
		$this->write_log('order_auto_refund');
	}

	//使优惠券失效，过期的现金券状态为-2
	public function unvalid_coupon() {
		$now = time();
		$sql = "SELECT * FROM {$this->tbp}coupon WHERE status=1 AND end_time<'{$now}'";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $k=>$g) {
				$sql = "UPDATE {$this->tbp}coupon_sn SET status='-2' WHERE status=1 AND coupon_id='{$g->id}'";
				$this->db->query($sql);
			}
		}
		$this->write_log('unvalid_coupon');
	}
	
	private function write_log($content) {
		$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
		fwrite($log, date("Y-m-d H:i:s")." {$content}\n\r");
		fclose($log);
	}
}
